package cn.iocoder.yudao.module.todo.service.tasksparticipants;

import cn.iocoder.yudao.module.todo.controller.admin.tasks.vo.TasksParticipantsPageReqVO;
import cn.iocoder.yudao.module.todo.controller.admin.tasks.vo.TasksParticipantsSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;


import cn.iocoder.yudao.module.todo.dal.dataobject.tasksparticipants.TasksParticipantsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.todo.dal.mysql.tasksparticipants.TasksParticipantsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.todo.enums.ErrorCodeConstants.*;

/**
 * 待办任务参与者 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TasksParticipantsServiceImpl implements TasksParticipantsService {

    @Resource
    private TasksParticipantsMapper tasksParticipantsMapper;

    @Override
    public String createTasksParticipants(TasksParticipantsSaveReqVO createReqVO) {
        // 插入
        TasksParticipantsDO tasksParticipants = BeanUtils.toBean(createReqVO, TasksParticipantsDO.class);
        tasksParticipantsMapper.insert(tasksParticipants);
        // 返回
        return tasksParticipants.getTaskId();
    }

    @Override
    public void updateTasksParticipants(TasksParticipantsSaveReqVO updateReqVO) {
        // 校验存在
        validateTasksParticipantsExists(updateReqVO.getTaskId());
        // 更新
        TasksParticipantsDO updateObj = BeanUtils.toBean(updateReqVO, TasksParticipantsDO.class);
        tasksParticipantsMapper.updateById(updateObj);
    }

    @Override
    public void deleteTasksParticipants(String id) {
        // 校验存在
        validateTasksParticipantsExists(id);
        // 删除
        tasksParticipantsMapper.deleteById(id);
    }

    private void validateTasksParticipantsExists(String id) {
        if (tasksParticipantsMapper.selectById(id) == null) {
            throw exception(TASKS_PARTICIPANTS_NOT_EXISTS);
        }
    }

    @Override
    public TasksParticipantsDO getTasksParticipants(String id) {
        return tasksParticipantsMapper.selectById(id);
    }

    @Override
    public PageResult<TasksParticipantsDO> getTasksParticipantsPage(TasksParticipantsPageReqVO pageReqVO) {
        return tasksParticipantsMapper.selectPage(pageReqVO);
    }

}